﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerSkillButton : MonoBehaviour
{
    private bool isFinish = true;
    private float cdTime = 0.0f;

    public Image[] AttackCD;
    public int CDImageNum;
    
    void Update()
    {
        if (PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Death && isFinish)
        {
            GetButtonDownState();
        }
    }

    private void GetButtonDownState()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            PlayerMouseTouch.Instance().vtTargetPos = transform.position;
            if (PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack2 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack3 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack4 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Gun)
            {
                PlayerAnimatorController.Instance().PlayerAttack1();
                isFinish = false;
                cdTime = PlayerState.Instance().SkillCD1.SkillCDTime;
                CDImageNum = 0;
                AttackCD[CDImageNum].fillAmount = 1;
                StartCoroutine(SkillEnd(CDImageNum, cdTime, cdTime));
            }
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            PlayerMouseTouch.Instance().vtTargetPos = transform.position;
            if (PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack1 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack3 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack4 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Gun)
            {
                PlayerAnimatorController.Instance().PlayerAttack2();
                isFinish = false;
                cdTime = PlayerState.Instance().SkillCD2.SkillCDTime;
                CDImageNum = 1;
                AttackCD[CDImageNum].fillAmount = 1;
                StartCoroutine(SkillEnd(CDImageNum, cdTime, cdTime));
            }
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            PlayerMouseTouch.Instance().vtTargetPos = transform.position;
            if (PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack1 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack2 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack4 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Gun)
            {
                PlayerAnimatorController.Instance().PlayerAttack3();
                isFinish = false;
                cdTime = PlayerState.Instance().SkillCD3.SkillCDTime;
                CDImageNum = 2;
                AttackCD[CDImageNum].fillAmount = 1;
                StartCoroutine(SkillEnd(CDImageNum, cdTime, cdTime));
            }
        }
        else if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            PlayerMouseTouch.Instance().vtTargetPos = transform.position;
            if (PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack1 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack2 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack3 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Gun)
            {
                PlayerAnimatorController.Instance().PlayerAttack4();
                isFinish = false;
                cdTime = PlayerState.Instance().SkillCD4.SkillCDTime;
                CDImageNum = 3;
                AttackCD[CDImageNum].fillAmount = 1;
                StartCoroutine(SkillEnd(CDImageNum, cdTime, cdTime));
            }
        }
        else if (Input.GetKeyDown(KeyCode.Space))
        {
            PlayerMouseTouch.Instance().vtTargetPos = transform.position;
            if (PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack1 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack2 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack3 &&
                PlayerState.Instance().PlayerStateNow != PlayerStateEnum.Attack4)
            {
                PlayerAnimatorController.Instance().PlayerGun();
            }
        }
    }

    IEnumerator SkillEnd(int cdImageNum, float timesTemp, float times)
    {
        while (timesTemp > 0)
        {
            timesTemp -= 1 * Time.deltaTime;
            AttackCD[cdImageNum].fillAmount = timesTemp / times;
            yield return 0;
        }
        yield return new WaitForSeconds(times);
        isFinish = true;
    }

    public void PlayerAttackEnd()
    {
        PlayerAnimatorController.Instance().PlayerAttackEnd();
    }
}
